//
//  LHHomeController.swift
//  Swift3.0WB
//
//  Created by 美食家 on 17/3/28.
//  Copyright © 2017年 不孤独的美食家. All rights reserved.
//

import UIKit

private let cellID="cellID"

class LHHomeController: LHBaseController {
    
    lazy var statusVM = LHStatusListViewModel()
    
//    override func viewDidLoad() {
//        super.viewDidLoad()
//        loadData()
//    }
    
    func LeftBtnClick() -> Void {
        print("点击显示好友群")
        let vc=LHDemoController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    override func loadData() -> Void{
        
        
      statusVM.loadStatusLit(pullup: self.isPullup) { (isSuccess,shouldRefresh) in
        
        
        
        if isSuccess{
            print("加载结束最后一条微博文字\(self.statusVM.statusList.last?.text)")
            self.refreshControll?.endRefreshing()
            self.isPullup=false
            if shouldRefresh{
            self.tableView?.reloadData()
            }
            

        }else{
            print("没有最新数据")
        }
        
        }
        
    }

}

// MARK: - 数据源方法
extension LHHomeController{
 
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusVM.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=tableView.dequeueReusableCell(withIdentifier: cellID)
        
            cell?.textLabel?.text=statusVM.statusList[indexPath.row].text

        return cell!
    }
    
}

// MARK: - 设置UI
extension LHHomeController{

    override func setupTableView() {
        super.setupTableView()
        navItem.leftBarButtonItem=UIBarButtonItem(title: "好友群", target: self, action: #selector(LeftBtnClick),isBack:false)
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        setupNaviTitle()
    }
    
    private func setupNaviTitle() {
        
        let btn = WBTitleButton(title: LHNetWorkManager.shared.userAccount.screen_name)
        navItem.titleView = btn
        btn.addTarget(self, action: #selector(clickTitleAction), for: .touchUpInside)
    
    }
    
    @objc func clickTitleAction(btn:UIButton){
    
        btn.isSelected = !btn.isSelected
    }
}
